package com.goods.shop.controller;

import com.goods.shop.BaseController;
import com.goods.shop.service.constants.WeiXinUrlFinals;
import com.goods.shop.service.model.wxmessage.WxTextModel;
import com.goods.shop.util.WeiXinUtils;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

@RestController
public class WeiXinOauthController extends BaseController {
    //http://www.ru-tourism.com/oauth/receive
    @RequestMapping("/weixin/oauth/receive")
    public void weixin(Model model, HttpServletRequest request, HttpServletResponse response){
        StringBuilder url = new StringBuilder();
        url.append(WeiXinUrlFinals.AUTHORIZEURL).append("?appid=").append(WeiXinUrlFinals.APPIDTEST);
        try {
            //网页授权的静默授权snsapi_base
            String directUrl = URLEncoder.encode("http://www.ru-tourism.com/weixin/oauth/dest", "utf-8");
            url.append("&redirect_uri=").append(directUrl);
            url.append("&response_type=code&scope=snsapi_base&state=123#wechat_redirect");

            response.sendRedirect(url.toString());
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    @RequestMapping("/weixin/oauth/dest")
    public String dest(Model model, HttpServletRequest request, HttpServletResponse response){
        String code = request.getParameter("code");
        String opendId= WeiXinUtils.getOauthUserInfo(WeiXinUrlFinals.APPIDTEST,WeiXinUrlFinals.SECRETTEST,code);
        return opendId;
    }
}
